package com.windfone.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.windfone.common.StringUtil;
import com.windfone.model.Member;
import com.windfone.model.MemberDetail;
import com.windfone.service.MemberService;

public class RegisterAction extends ActionSupport {
	/**	 */
	private static final long serialVersionUID = 1L;
	private MemberService memberService;
	private String name;
	private String cardId;
	private MemberDetail memberDetail;
	private String password;
	private String loginName;
	private String email;
	private String rtvMsg;
	public String getRtvMsg() {
		return rtvMsg;
	}
	public void setRtvMsg(String rtvMsg) {
		this.rtvMsg = rtvMsg;
	}
	public MemberService getMemberService() {
		return memberService;
	}
	public String getName() {
		return name;
	}
	public String getCardId() {
		return cardId;
	}
	public MemberDetail getMemberDetail() {
		return memberDetail;
	}
	public String getPassword() {
		return password;
	}
	public String getLoginName() {
		return loginName;
	}
	public String getEmail() {
		return email;
	}
	public boolean isExistFlag() {
		return existFlag;
	}
	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setCardId(String cardId) {
		this.cardId = cardId;
	}
	public void setMemberDetail(MemberDetail memberDetail) {
		this.memberDetail = memberDetail;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public void setExistFlag(boolean existFlag) {
		this.existFlag = existFlag;
	}
	private boolean existFlag = false;
 
	public String addRegisterUser(){
		rtvMsg = "";
		int flag = 0;
		int flag2 = 0;
		Member m = new Member();
		m.setLoginName(loginName);
		if ( password !=null && !password.equals("")){
			m.setPassword(  StringUtil.encode(password) );
			if ( memberService.checkMember(m)==false)
				flag= memberService.addRegisterUser( m );
		}
		if (flag ==0 ){
			System.out.println("add user register don't success"+loginName );
		}
		if ( flag == 1){
			Member m2 = memberService.getMemberByLoginName(m);
			memberDetail = new MemberDetail();
			memberDetail.setEmail(email);
			memberDetail.setMemberId(m2.getId());
			memberDetail.setSex(-1);
			flag2=memberService.addRegisterDetailUser(memberDetail);
		}
		if ( flag ==1 && flag2==1){
			rtvMsg = "注册成功";
		}
		return SUCCESS;
	}
	public String orderRegisterUser(){
		rtvMsg = "";
		int flag = 0;
		int flag2 = 0;
		Member m = new Member();
		m.setLoginName(loginName);
		if ( password !=null && !password.equals("")){
			m.setPassword(  StringUtil.encode(password) );
			if ( memberService.checkMember(m)==false)
				flag= memberService.addRegisterUser( m );
			String tmp = StringUtil.encode(password);
			Member memberBean= memberService.loginByLoginName(loginName, tmp);
			if ( memberBean != null ){
				HttpServletRequest request = ServletActionContext.getRequest();
				HttpSession session = request.getSession();
				session.setAttribute("userBean", memberBean);
				int i=memberService.updateLastLogin(memberBean);
			}
		}
		if (flag ==0 ){
			System.out.println("add user register don't success"+loginName );
		}
		if ( flag == 1){
			Member m2 = memberService.getMemberByLoginName(m);
			memberDetail = new MemberDetail();
			memberDetail.setEmail(email);
			memberDetail.setMemberId(m2.getId());
			memberDetail.setSex(-1);
			flag2=memberService.addRegisterDetailUser(memberDetail);
		}
		if ( flag ==1 && flag2==1){
			rtvMsg = "注册成功";
		}
		return SUCCESS;
	}

}
